<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<?php
// check if the user is logged-in
include 'authentication.php';
isAuthorized();

$parent_dir = $_GET['parent_dir'];
$idc = $_GET['idc'];

include_once($_SERVER["DOCUMENT_ROOT"]."/web-doc/protected/database/MySqlDAO.php");
include_once("../utils/redirect.php");
include_once('../utils/directories.php'); // Contain variable "rootDir"

$mySqlDAO = new MySqlDAO();
            
if (!empty($idc)) {    
    if (!empty($_POST['isToInsert'])) {
        if (!empty($_POST['name'])) {
            $name = $_POST['name'];
            
            // Controllo se esiste una categoria con lo stesso nome nella parent_dir
            $idcres =  $mySqlDAO->checkCategoryName($parent_dir, $name);
            if(!empty($idcres)) {
                echo "ATTENZIONE: Il nome <b>$name</b> è già usato per un'altra categoria. Scegliere un'altro nome.<br>";
            }
            else {
                
                $oldDBPath = $mySqlDAO->getCategoryPath($idc);
                $oldPath = getRootDir() . $oldDBPath;
                $oldname = basename($oldPath);
                $newPath = substr($oldPath, 0, $oldPath.  strlen($oldPath) - strlen($oldname . DIRECTORY_SEPARATOR)). $name . DIRECTORY_SEPARATOR;

                $newDBPath = substr($oldDBPath, 0, strlen($oldDBPath) - strlen($oldname . DIRECTORY_SEPARATOR)). $name . DIRECTORY_SEPARATOR;

                $mySqlDAO->modifyCategory($idc, $name, $newDBPath, $parent_dir);
                rename($oldPath, $newPath);

                redirect("showcategory.php?idc=$parent_dir",FALSE);
            }
        } else {
            echo "ATTENZIONE: Inserire tutti i dati.";
        }
    }
} else {
    die("ERRORE: Parent directory mancante. <a href=javascript:history.back(-1)>Indietro</a>");
}

$name = $mySqlDAO->getCategoryName($idc);

?>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        
        <link rel="stylesheet" type="text/css" href="../styles/main.css">
        <link rel="stylesheet" type="text/css" href="../styles/admin.css">
        <title>Web-Doc - Modify category</title>
    </head>
    <body>
        <?php
            $formaction = "modifycategory.php?parent_dir=$parent_dir&idc=$idc";
        ?>
        <form method="post" enctype="multipart/form-data" action="<?php echo $formaction ?>">
            <table width="450" border="0" cellpadding="1" cellspacing="1" class="box">
                <tr>
                    <td valign="top" colspan="2">
                        Nome: <input type="text" name="name" size="50" maxlength="150" value="<?php echo $name ?>">
                        <input type="hidden" name="isToInsert" value="true">
                    </td>
                </tr>
                <tr>
                    <td width="80" colspan="2">
                        <br><br>
                        <input type="submit" value="Modifica" />
                        <!--<input name="upload" type="submit" class="box" id="upload" value=" Upload ">-->
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

